/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.service;

import java.io.File;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.util.OfficeToolExcel;
import javacommon.util.StringUtils;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.tech15.pub.dao.PubCostDao;
import com.tech15.pub.model.PubCost;
import com.tech15.pub.vo.query.PubCostQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class PubCostManager extends BaseManager<PubCost,java.lang.String>{

	private PubCostDao pubCostDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setPubCostDao(PubCostDao dao) {
		this.pubCostDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.pubCostDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(PubCostQuery query) {
		Page page = pubCostDao.findPage(query);
		List<PubCost> list = page.getResult();
		for(PubCost c : list){
			 c.setHj(c.getY1()+c.getY2()+c.getY3()+c.getY4()+c.getY5()+c.getY6()+c.getY7()+c.getY8()+c.getY9()+c.getY10()+c.getY11()+c.getY12());
			 c.setQnsyed(c.getQnkbxed() - c.getHj());
		}
		return page;
	}
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		 Page page = pubCostDao.findByPageRequest(pr);
		 return page;
	}
	public String daoruTemplate(PubCost pubCost,File attachFile) {
		Map map = OfficeToolExcel.readExcelByCost(attachFile);
		
		String msg = (String)map.get("msg");
		if(StringUtils.equals("true",msg)){
			List<PubCost> list = (List<PubCost>) map.get("data");
			if(list != null){
				for(PubCost cost : list){
					if(StringUtils.notNull(cost.getYgbh())&&StringUtils.notNull(cost.getYgxm())){
						cost.setNd(pubCost.getNd());
						cost.setLb(pubCost.getLb());
						pubCostDao.save(cost);
					}
				}
			}
			return "true";
		}else{
			return msg;
		}
	}
	
}
